#include "texture_manager.h"
#include "game.h"
#include <SDL_image.h>
#include <iostream>

TextureManager* TextureManager::s_pInstance = NULL;

bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer)
{
    SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
    
    if(pTempSurface == NULL)
    {
        std::cout << IMG_GetError() << std::endl;
        return false;
    }
    
    SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
    
    SDL_FreeSurface(pTempSurface);
    
    if(pTexture != NULL)
    {
        m_textureMap[id] = pTexture;
        return true;
    }
    
    return false;
}

//void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip)
//{
//    SDL_Rect srcRect;
//    SDL_Rect destRect;
//    
//    srcRect.x = 0;
//    srcRect.y = 0;
//    srcRect.w = destRect.w = width;
//    srcRect.h = destRect.h = height;
//    destRect.x = x;
//    destRect.y = y;
//    
//    SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip);
//}

void TextureManager::drawFrame(std::string id, int x, int y, int width,
    int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, double angle, int alpha, double scale, SDL_RendererFlip flip)
{
    SDL_Rect srcRect;
    SDL_Rect destRect;
    srcRect.x = width * currentFrame;
    srcRect.y = height * currentRow;
    srcRect.w = width;
    srcRect.h = height;
    destRect.x = x;
    destRect.y = y;
    destRect.w = static_cast<int>(srcRect.w * scale);
    destRect.h = static_cast<int>(srcRect.h * scale);
    
    SDL_SetTextureAlphaMod(m_textureMap[id], static_cast<Uint8>(alpha));
    SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, angle, NULL, flip);
}

void TextureManager::drawTile(std::string id, int margin, int spacing,
    int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer)
{
    SDL_Rect srcRect;
    SDL_Rect destRect;
    srcRect.x = margin + (spacing + width) * currentFrame;
    srcRect.y = margin + (spacing + height) * currentRow;
    srcRect.w = destRect.w = width;
    srcRect.h = destRect.h = height;
    destRect.x = x;
    destRect.y = y;
    
    SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, SDL_FLIP_NONE);
}


void TextureManager::clearTextureMap()
{
    m_textureMap.clear();
}

void TextureManager::clearFromTextureMap(std::string id)
{
    m_textureMap.erase(id);
}

void TextureManager::drawGrids() { // TODO: Query backgound texture to get x, y
    SDL_Renderer* renderer = TheGame::Instance()->getRenderer();
    // player field
    for (int x = 25; x <= 375; x += 35)
        SDL_RenderDrawLine(renderer, x, 150, x, 500);
    for (int y = 150; y <= 500; y += 35)
        SDL_RenderDrawLine(renderer, 25, y, 375, y);
    // enemy field
    for (int x = 425; x <= 775; x += 35)
        SDL_RenderDrawLine(renderer, x, 150, x, 500);
    for (int y = 150; y <= 500; y += 35)
        SDL_RenderDrawLine(renderer, 425, y, 775, y);
}

//bool TextureManager::load(std::string fontFile, int ptsize, std::string text,
//    Uint8 r, Uint8 g, Uint8 b, std::string id, SDL_Renderer* pRenderer)
//{
//    if (TTF_Init() == -1) {
//        std::cout << TTF_GetError() << std::endl;
//        TTF_Quit();
//        return false;
//    }
//    TTF_Font *font = TTF_OpenFont(fontFile.c_str(), ptsize);
//    if (font == NULL) {
//        std::cout << TTF_GetError() << std::endl;
//        TTF_Quit();
//        return false;
//    }
//    SDL_Color fg = {r, g, b};
//    SDL_Surface* pTempSurface = TTF_RenderText_Blended(font, text.c_str(), fg);
//    
//    if (pTempSurface == NULL)
//    {
//		std::cout << TTF_GetError() << std::endl;
//        TTF_Quit();
//        return false;
//    }
//    
//    SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
//    
//    SDL_FreeSurface(pTempSurface);
//    
//    if (pTexture != NULL)
//    {
//        m_textureMap[id] = pTexture;
//        TTF_Quit();
//        return true;
//    }
//    
//    TTF_Quit();
//    return false;
//}
